home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindlink.net!news
- From: genew@mindlink.bc.ca (Gene Wirchenko)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer-to-Double as Function Arg
- Date: Tue, 16 Jan 1996 10:49:24 GMT
- Organization: MIND LINK! - British Columbia, Canada
- Message-ID: <4dfvu4$q18@fountain.mindlink.net>
- References: <4dfccl$j5h@colossus.holonet.net>
- NNTP-Posting-Host: line023.nwm.mindlink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- mitch@news.mdli.com (Mitch Miller) wrote:
-
- >I'm having a problem with a function that takes a
- >couple of pointer-to-double arguments, initializes then
- >and assigns values to them in an array style:
-
- >Function is something like:
-
- > int GetValue( long iDim, double ** Values1, double **
- Above line changed from "*" to "**" twice
- > Values2 )
- > {
- > int iter;
- > *Values1 = (double *) malloc( iDim * sizeof( double ));
- Dereference Values1
- > if (Values1 == NULL )
- > ....
- > *Values2 = (double *) malloc( iDim * sizeof( double ));
- Dereference Values2
- > if (Values2 == NULL )
- > ...
-
- > for (iter=1; iter<iDim; iter++)
- > {
- > *Values1[iter] = ...;
- Dereference Values1
- > *Values2[iter] = ...;
- Dereference Values2
- > }
-
- > return 1;
- > }
-
- >Call from main:
-
- > double ** Ptr1;
- Double indirection
- > double ** Ptr2;
- Double indirection
- > if (!GetValue( iSize, Ptr1, Ptr2 ) )....
-
-
- >When I look at the values of Values1 and Values2 in GetValue, they have
- >the correct values.
-
- Yes, call by value allows the LOCAL parameter value to be
- changed...
-
- >However, back in main, they have NULL values.
-
- ...but does not change the actual parameter. It might not be a
- NULL value, could be anything.
-
- >If I make Ptr1 and Ptr2 global variables so that they are not included
- >in the function calls, the code works fine.
-
- Yes, then you aren't using calling, so call by value doesn't get
- you.
-
- >I've checked the FAQs, but couldn't find anything quite like this.
-
- It's probably in there somewhere.
-
- >Thanks in advance...
-
- In C, parameters are passed by value. In order to modify a var
- in a function and have it stick, the var must be global or if a parm,
- it must be a pointer to the var.
- So for a pointer to double to be changeable, call with..."All
- together, class!"... a pointer to pointer to double.
-
- Sincerely,
-
- Gene Wirchenko
-
- C Pronunciation Guide:
- y=x++; "wye equals ex plus plus semicolon"
- x=x++; "ex equals ex doublecross semicolon"
-
-